
var NodeRSA = require('node-rsa');

var Class = require('ClassBuilder');

var RSAHelper = Class(function( _public ) {

    _public._construct = function( clientPem, serverPem ) {

        this.clientPem = clientPem;
        this.serverPem = serverPem;

        this.pubKey = new NodeRSA( clientPem, 'pkcs1-public-pem' );
        this.priKey = new NodeRSA( serverPem, 'pkcs1-private-pem' );

    };

    _public.getPublicKey = function() {
        return this.clientPem;
    };

    _public.encrypt = function( input, outputEncoding ) {
        outputEncoding = outputEncoding || 'buffer';
        return this.pubKey.encrypt( input, outputEncoding, 'utf-8' );
    };

    _public.decrypt = function( encryptedStringOrBuffer, outputEncoding ) {

        var output = this.priKey.decrypt( encryptedStringOrBuffer, outputEncoding );

        return output;

    };

});


module.exports = RSAHelper;